using System;
using PaperPlaneTools;
using UnityEngine;
using Voodoo.Sauce.Common.Utils;

namespace Voodoo.Sauce.Internal.AppRater
{
	public class AndroidAppRater : MonoBehaviour, IAlertPlatformAdapter
	{
		private const string TAG = "AndroidAppRater";

		private const string UNITY_ACTIVITY_CLASS = "com.unity3d.player.UnityPlayer";

		private const string VOODOO_APP_RATER_CLASS = "io.voodoo.voodooapprater.AppRater";

		private const string VOODOO_EMAIL_ADDRESS = "support@voodoo.io";

		private Action _positiveCallback;

		private Action _neutralCallback;

		private Action _negativeCallback;

		private Action _dismissCallback;

		private void Awake()
		{
			base.gameObject.name = "VoodooAppRater";
			if (PlatformUtils.UNITY_ANDROID && !PlatformUtils.UNITY_EDITOR)
			{
				VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "Setting Android's AppRater custom alert");
				AppRater.SetCustomAlert(this);
			}
		}

		public void SetOnDismiss(Action action)
		{
			_dismissCallback = action;
		}

		public void Show(Alert alert)
		{
			VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "Showing rate pop up.");
			ShowNativeAlertDialog();
			_positiveCallback = alert.PositiveButton?.Handler;
			_neutralCallback = alert.NeutralButton?.Handler;
			_negativeCallback = alert.NegativeButton?.Handler;
		}

		public void Dismiss()
		{
			VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "Dismissing rate pop up.");
			_dismissCallback?.Invoke();
		}

		public void HandleEvent(string eventName, string value)
		{
			VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "Native event : " + eventName + " -> " + value);
		}

		private static void ShowNativeAlertDialog()
		{
			AndroidJavaObject @static = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
			new AndroidJavaClass("io.voodoo.voodooapprater.AppRater").CallStatic("Show", @static, @static, "Enjoying " + Application.productName + "?", "Tap a star to rate it on the store.", "Submit", "Not Now");
		}

		public void OnPositiveButtonClick(string ratingString)
		{
			int num = int.Parse(ratingString);
			VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "OnPositiveButtonClick: " + num);
			if (num >= 4)
			{
				_positiveCallback?.Invoke();
			}
			else
			{
				_negativeCallback?.Invoke();
				Application.OpenURL("mailto:support@voodoo.io?subject=" + Application.productName + "%20Issue&body=Tell%20us%20what%20you%20didn't%20like%20:%20");
			}
			Dismiss();
		}

		public void OnNeutralButtonClick()
		{
			VoodooLog.LogDebug(Module.APP_RATER, "AndroidAppRater", "OnNeutralButtonClick");
			_neutralCallback?.Invoke();
			Dismiss();
		}
	}
}
